﻿using System;
using System.IO;
using System.IO.IsolatedStorage;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Phone.Tasks;

namespace tnet.Controls.Lookless
{
    public class Tools
    {
        public static String ScrubDate(String InputDate, String TagValue)
        {
            return InputDate.Substring(0, InputDate.LastIndexOf(' ')) + TagValue;
        }

        public static void SendTwitter(String RefUrl, String TweetText, String UserName )
        {
            //twitter.com/share?url=[URL]&text=[TEXT]&via=[username]"
            Tools.LaunchURL("http://www.twitter.com/share?url=" + RefUrl + "&via=" + UserName + "&text=" + TweetText);
        }

        public static void SendToFacebook(String RefUrl, String ContentText)
        {
            //www.facebook.com/sharer.php?u=[URL]&t=[CONTENT]
            Tools.LaunchURL("http://www.facebook.com/sharer.php?u=" + RefUrl + "&t=" + ContentText); 
        }

        public static void LaunchURL(String ThisUrl)
        {
            try
            {
                WebBrowserTask webBrowserTask = new WebBrowserTask();
                webBrowserTask.Uri = new Uri(ThisUrl);
                webBrowserTask.Show();
            }
            catch (Exception) { } 
        } 

        public static void SendEmail(String BodyText, String SubjectText, String ToEmail)
        { 
            EmailComposeTask emailComposeTask = new EmailComposeTask();
            emailComposeTask.Body = BodyText;
            emailComposeTask.Subject = SubjectText;
            emailComposeTask.To = ToEmail;
            emailComposeTask.Show();  
        }

        public static void SendEmail(String BodyText, String SubjectText)
        {
            SendEmail(BodyText, SubjectText, String.Empty); 
        }

        public static String ScrubTitleToKey(String InputValue)
        {
            InputValue = InputValue.Replace("’", "");
            InputValue = InputValue.Replace("‘", "");
            InputValue = InputValue.Replace(" ", "");
            InputValue = InputValue.Replace("&", "");
            InputValue = InputValue.Replace("_", "");
            InputValue = InputValue.Replace("'", "");
            InputValue = InputValue.Replace("\"", "");
            InputValue = InputValue.Replace("*", "");
            InputValue = InputValue.Replace("(", "");
            InputValue = InputValue.Replace(")", "");
            InputValue = InputValue.Replace("[", "");
            InputValue = InputValue.Replace("!", "");
            InputValue = InputValue.Replace("`", "");
            InputValue = InputValue.Replace("~", "");
            InputValue = InputValue.Replace("@", "");
            InputValue = InputValue.Replace("#", "");
            InputValue = InputValue.Replace("$", "");
            InputValue = InputValue.Replace("%", "");
            InputValue = InputValue.Replace("^", "");
            InputValue = InputValue.Replace("-", "");
            InputValue = InputValue.Replace("=", "");
            InputValue = InputValue.Replace("+", "");
            InputValue = InputValue.Replace("{", "");
            InputValue = InputValue.Replace("}", "");
            InputValue = InputValue.Replace("|", "");
            InputValue = InputValue.Replace("\\", "");
            InputValue = InputValue.Replace(":", "");
            InputValue = InputValue.Replace(";", "");
            InputValue = InputValue.Replace("'", "");
            InputValue = InputValue.Replace("<", "");
            InputValue = InputValue.Replace(">", "");
            InputValue = InputValue.Replace(".", "");
            InputValue = InputValue.Replace(",", "");
            InputValue = InputValue.Replace("?", "");
            InputValue = InputValue.Replace("/", "");
            return InputValue;
        }

        public static void PlaySound(String SoundPath)
        {
            Stream _stream = TitleContainer.OpenStream(SoundPath);
            SoundEffect _effect = SoundEffect.FromStream(_stream);
            FrameworkDispatcher.Update();
            _effect.Play();
        }

        public static SoundEffect PlaySoundReturn(String SoundPath)
        {
            Stream _stream = TitleContainer.OpenStream(SoundPath);
            SoundEffect _effect = SoundEffect.FromStream(_stream);
            FrameworkDispatcher.Update();
            _effect.Play();
            return _effect;
        }

        public static bool IsNaN(string DoubleAmount)
        {
            try
            {
                double Temp = double.Parse(DoubleAmount);
                if (Temp > 0)
                {
                    return false;
                }
            }
            catch (Exception) { }
            return true;
        }

        public static int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

        public static System.Windows.Media.Color GetColor(string ThisColorCode)
        {
            System.Windows.Media.Color c = new System.Windows.Media.Color();
            switch (ThisColorCode.Length)
            {
                case 6:
                    c.A = Convert.ToByte(255);
                    c.R = Convert.ToByte(ThisColorCode.Substring(0, 2), 16);
                    c.G = Convert.ToByte(ThisColorCode.Substring(2, 2), 16);
                    c.B = Convert.ToByte(ThisColorCode.Substring(4, 2), 16);
                    break;
                case 7:
                    c.A = Convert.ToByte(255);
                    c.R = Convert.ToByte(ThisColorCode.Substring(1, 2), 16);
                    c.G = Convert.ToByte(ThisColorCode.Substring(3, 2), 16);
                    c.B = Convert.ToByte(ThisColorCode.Substring(5, 2), 16);
                    break;
                case 8:
                    c.A = Convert.ToByte(ThisColorCode.Substring(0, 2), 16);
                    c.R = Convert.ToByte(ThisColorCode.Substring(2, 2), 16);
                    c.G = Convert.ToByte(ThisColorCode.Substring(4, 2), 16);
                    c.B = Convert.ToByte(ThisColorCode.Substring(6, 2), 16);
                    break;
                case 9:
                    c.A = Convert.ToByte(ThisColorCode.Substring(1, 2), 16);
                    c.R = Convert.ToByte(ThisColorCode.Substring(3, 2), 16);
                    c.G = Convert.ToByte(ThisColorCode.Substring(5, 2), 16);
                    c.B = Convert.ToByte(ThisColorCode.Substring(7, 2), 16);
                    break;
            }
            return c;
        }

        public static String GetSetting(String SomeValue)
        {
            // get saved setting
            StreamReader sr = new StreamReader(new IsolatedStorageFileStream(SomeValue, FileMode.OpenOrCreate, IsolatedStorageFile.GetUserStoreForApplication()));
            String ALine = sr.ReadToEnd();  
            sr.Close(); 
            return ALine;
        }

        public static void SaveSetting(String KeyValue, String SomeValue)
        {
            StreamWriter MyWriter = new StreamWriter(new IsolatedStorageFileStream(KeyValue, FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication()));
            MyWriter.Write(SomeValue);
            MyWriter.Close();
        }
    }
}
